import { Observable, merge, switchMap, asapScheduler, timer, asyncScheduler, Subject, of, empty, EMPTY } from 'rxjs';

/**
 * 注意：触发到调用，中间仍旧会有update的执行。
 */

export function MergeWithTimer(observables: Observable<any>[]): Observable<any> {
    return merge(...observables).pipe(switchMap(() => timer(null, null, asapScheduler)));
}

export function MergeWithTimerASYNC(observables: Observable<any>[]): Observable<any> {
    return merge(...observables).pipe(switchMap(() => timer(null, null, asyncScheduler)));
}

// 简单合并(适用于ReactiveProperty等带有_subject的继承类型)
export function Merge(...rps: { _subject: Subject<any> }[]) {
    return merge(...rps.map(_ => _._subject));
}

// MergeWithTimer的参数含_subject类型版本
export function MergeASAP(...rps: { _subject: Subject<any> }[]) {
    return merge(...rps.map(_ => _?._subject ?? of(null))).pipe(switchMap(() => timer(null, null, asapScheduler)));
}

// MergeWithTimerASYNC的参数含_subject类型版本
export function MergeASYNC(...rps: { _subject: Subject<any> }[]) {
    return merge(...rps.map(_ => _._subject ?? of(null))).pipe(switchMap(() => timer(null, null, asyncScheduler)));
}